package com.fzt.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fzt.system.domain.entity.SysMenuEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单mapper
 * </p>
 *
 * @author 非洲铜
 * @since 2022-12-08
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenuEntity> {

    /**
     * 查询全部 authority
     *
     * @return List<String>
     */
    default Set<String> getAuthorities() {
        return this.selectList(
                        Wrappers.<SysMenuEntity>lambdaQuery().
                                eq(SysMenuEntity::getDeleted, 0)
                                .isNotNull(SysMenuEntity::getAuthority)
                                .select(SysMenuEntity::getAuthority)
                )
                .stream()
                .map(SysMenuEntity::getAuthority)
                .filter(StringUtils::isNotEmpty)
                .collect(Collectors.toSet());
    }

    /**
     * 查询用户的 authority
     *
     * @param userId 用户ID
     * @return List<String>
     */
    List<String> getUserAuthorities(Long userId);

    /**
     * 获取全部执行类型的菜单
     *
     * @param type 0：菜单   1：按钮   2：接口
     * @return List<SysMenuEntity>
     */
    default List<SysMenuEntity> getMenuList(Integer type) {
        return this.selectList(
                Wrappers.<SysMenuEntity>lambdaQuery()
                        .eq(Objects.nonNull(type), SysMenuEntity::getType, type)
                        .eq(SysMenuEntity::getDeleted, 0)
        );
    }

    List<SysMenuEntity> getUserMenuList(Integer type);
}
